const connection = require("../app/database");

class UploadService {
    // 创建文件记录
    async created(url, filename, mimetype, size, categoryId, remark) {
        try {
            // 拼接 SQL 语句
            const statement = 'INSERT INTO avatar (url, filename, mimetype, size, category_id,remark) VALUES (?,?,?,?,?,?);';
            // console.log(statement)

            // 执行 SQL 语句
            const [result] = await connection.execute(statement, [url, filename, mimetype, size, categoryId, remark]);

            return result;
        } catch (error) {
            console.error('FileService - created:', error);
            throw error; // 抛出异常，让调用者处理
        }
    }

    // 根据用户ID查找头像信息
    async findPhotoById(id) {
        try {
            const statement = 'SELECT * FROM `avatar` WHERE id=?;';

            const [result] = await connection.execute(statement, [id]);
            // console.log('result.pop()=>', result.pop())

            return result.pop(); // 返回结果数组的最后一个元素
        } catch (error) {
            console.error('UserService - findAvatarById:', error);
            throw error; // 抛出异常，让调用者处理
        }
    }
}

module.exports = new UploadService();
